package top.jiangqiang.qianyi.common.async;

import org.dromara.hutool.core.thread.RejectPolicy;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 启用异步任务
 */
@EnableAsync
public class AsyncAutoConfiguration {

    @Bean
    public AsyncTaskExecutor asyncTaskExecutor() {
        ThreadPoolTaskExecutor asyncTaskExecutor = new ThreadPoolTaskExecutor();
        asyncTaskExecutor.setCorePoolSize(5);
        asyncTaskExecutor.setMaxPoolSize(10);
        asyncTaskExecutor.setQueueCapacity(20);
        asyncTaskExecutor.setKeepAliveSeconds(60);
        asyncTaskExecutor.setThreadNamePrefix("async-task-thread-pool");
        asyncTaskExecutor.setRejectedExecutionHandler(RejectPolicy.CALLER_RUNS.getValue());
        asyncTaskExecutor.initialize();
        return asyncTaskExecutor;
    }
}
